package com.evola.dbsm.view.script.edit
{
	import com.evola.dbsm.communication.Commands;
	import com.evola.dbsm.model.ProjectBean;
	import com.evola.dbsm.model.ScriptBean;
	import com.evola.dbsm.service.RemoteServiceFacade;
	import com.evola.dbsm.service.ServiceResult;
	
	import mx.controls.Alert;
	import mx.validators.Validator;

	[Bindable]
	public class ScriptEditPresenter
	{

		public var view:ScriptEditView;
		public var model:ScriptEditModel=new ScriptEditModel();

		public function ScriptEditPresenter()
		{

		}

		public function edit(project:ProjectBean, script:ScriptBean):void
		{

			model.project=project;

			RemoteServiceFacade.executeCommand(Commands.GET_OBJECT_COMMAND, script, function(result:ServiceResult):void
			{

				model.script=result.data as ScriptBean;
			});
		}

		public function add(project:ProjectBean):void
		{

			model.project=project;
			model.script=new ScriptBean();
		}

		public function cancel():void
		{

			Presenters.navigationPresenter.showProject(model.project);
		}

		public function saveASDASD():void
		{

			var results:Array=Validator.validateAll([view.valContent]);

			if (results.length > 0)
			{

				Alert.show("Please fill in the form correctly.", "Validation error");
			}
			else
			{



			}

		}

		public function save():void
		{

			if (model.isInsert)
				RemoteServiceFacade.executeCommand(Commands.ADD_OBJECT_COMMAND, model.script, function(result:ServiceResult):void
				{

				});
			else
				RemoteServiceFacade.executeCommand(Commands.EDIT_OBJECT_COMMAND, model.script, function(result:ServiceResult):void
				{

				});
		}

	}
}
